package com.ctv.my.customview.MaiCustomView.CanvasDraw;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.ctv.my.customview.R;


public class DrawLineActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btnDrawLong;//长线按钮
    private Button btnDrawShort;//画短线按钮
    private Button btnDrawRedLine;//画红线按钮
    private Button btnDrawYellowLine;//画黄线按钮
    private Button btnClear;
    private Button btnSave;
    private DrawView drawView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_draw_line);
        initViewAndEvent();
    }

    private void initViewAndEvent() {
        btnDrawLong = (Button) findViewById(R.id.drawLong);
        btnDrawShort = (Button) findViewById(R.id.drawShort);
        btnDrawRedLine = (Button) findViewById(R.id.redLine);
        btnDrawYellowLine = (Button) findViewById(R.id.yellowLine);
        btnClear = (Button) findViewById(R.id.clear);
        btnSave = (Button) findViewById(R.id.save);
        drawView = (DrawView) findViewById(R.id.canvas);
        btnDrawLong.setOnClickListener(this);
        btnDrawShort.setOnClickListener(this);
        btnDrawRedLine.setOnClickListener(this);
        btnDrawYellowLine.setOnClickListener(this);
        btnClear.setOnClickListener(this);
        btnSave.setOnClickListener(this);
        RelativeLayout.LayoutParams para = (RelativeLayout.LayoutParams) drawView.getLayoutParams();
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        float width = dm.widthPixels * 0.8f;
        para.width = (int) width;
        para.height = (int) width;
        drawView.setLayoutParams(para);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.drawLong:
                //画长线
                drawView.setState(1);
                break;
            case R.id.drawShort:
                //画短线
                drawView.setState(2);
                break;
            case R.id.clear:
                //擦除
                drawView.setState(3);
                break;
            case R.id.redLine:
                //画红线
                drawView.setStateColor(4);
                break;
            case R.id.yellowLine:
                //画黄线
                drawView.setStateColor(5);
                break;
            case R.id.save:
                String path = drawView.savePng();
                if (path != null) {
                    Toast.makeText(this, path, Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, "保存失败，自己打开权限", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}
